

#include "EvalOutputListener.h"

using namespace std;

EvalOutputListener::EvalOutputListener() {
	detectedEvents = 0;
	processingTime = 0;
	numCalls = 0;
}

EvalOutputListener::~EvalOutputListener() { }

void EvalOutputListener::handleOutput(Outbox &outbox, double procTime) {
	detectedEvents += outbox.pkts.size();
	processingTime += procTime;
	numCalls++;

	cout << endl;
	cout << "**************************** OUTBOX ****************************" << endl;
	cout << "Clients:  ";
	if (!outbox.clients.empty()){
		for (std::set<int>::iterator it = outbox.clients.begin(); it != outbox.clients.end(); it++){
			cout << *it << " ";
		}
	} else {
		cout << "-";
	}
	cout << endl;
	cout << "Messages: ";
	if (!outbox.pkts.empty()){
		cout << endl;
		for (std::set<PubPkt*>::iterator it2 = outbox.pkts.begin(); it2 != outbox.pkts.end(); it2++){
			PubPkt* refPkt = *it2;
			cout << "\t  " << test::toString(refPkt) << endl;
		}
	} else{
		cout << "-" << endl;
	}
	cout << "****************************************************************" << endl << endl;
}

void EvalOutputListener::printToFile(std::string label, char const *fileName, bool newLine, bool printLabel) {
	//cout << "calls: " << numCalls << endl;
	//cout << "detectedEvents: " << detectedEvents << endl;

	ofstream file;
	file.open(fileName, ios::app);
	//double msgPerSec = duration==0 ? 0 : (detectedEvents/duration);
	if (newLine) file << "\n";
	if (printLabel) file << label << "\t";
	file << detectedEvents << "\t";
	file.close();

}

void EvalOutputListener::printMeanProcTime(std::string label, char const *fileName, bool newLine, bool printLabel) {
	ofstream file;
	file.open(fileName, ios::app);
	double mean = numCalls==0 ? 0 : (processingTime/numCalls);
	if (newLine) file << "\n";
	if (printLabel) file << label << "\t";
	file << mean << "\t";
	file.close();
}
